<template>
  <div class="order-container">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜素区域 -->
      <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" :clear="clearIpt" class="search">
        <el-button slot="append" icon="el-icon-search" @click="searchOrder"></el-button>
      </el-input>
      <!-- 订单表格区域 -->
      <el-table :data="orderlist" border stripe style="width: 100%; margin: 15px 0">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号"> </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="100"> </el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="100">
          <template #default="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="100"> </el-table-column>
        <el-table-column label="下单时间" width="200">
          <template #default="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <!-- 修改 -->
            <el-tooltip class="item" effect="light" content="修改订单地址" placement="bottom-start" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="editAddressDialogVisible = true"></el-button>
            </el-tooltip>
            <!-- 订单状态 -->
            <el-tooltip class="item" effect="light" content="订单状态" placement="bottom-start" :enterable="false">
              <el-button type="info" icon="el-icon-date" size="mini" @click="showDetailDialog(scope.row)"></el-button>
            </el-tooltip>
            <!-- 物流信息 -->
            <el-tooltip class="item" effect="light" content="物流信息" placement="bottom-start" :enterable="false">
              <el-button type="success" icon="el-icon-location" size="mini" @click="showProgressDialog(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页栏 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </el-card>

    <!-- 修改订单地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="editAddressDialogVisible" width="50%" @close="editAddressDialogClose">
      <el-form ref="editAddressFormRef" :model="editAddressForm" label-width="100px" :rules="editAddressFormRules">
        <el-form-item label="省市区/县" prop="address1">
          <!-- 级联选择器 -->
          <el-cascader v-model="editAddressForm.address1" :options="cityData" :props="editProps"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editAddressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流信息对话框 -->
    <el-dialog title="物流信息" :visible.sync="ProgressDialogVisible" width="50%">
      <!-- 时间线  -->
      <el-timeline :reverse="false">
        <el-timeline-item v-for="(item, i) in progresslist" :key="i" :timestamp="item.time">
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>

    <!-- 订单状态对话框 -->
    <el-dialog title="订单状态" :visible.sync="detailVisible" width="50%">
      <el-form ref="orderStateFormRef" :model="orderStateForm" label-width="100px">
        <el-form-item label="订单编号">
          <span>{{ orderStateById.order_number }}</span>
        </el-form-item>
        <el-form-item label="所购商品">
          <el-table :data="orderStateById.goods" border style="width: 100%">
            <el-table-column type="index" label="#"> </el-table-column>
            <el-table-column prop="goods_id" label="商品名称"> </el-table-column>
            <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
            <el-table-column prop="goods_number" label="商品数量"> </el-table-column>
            <el-table-column prop="goods_total_price" label="当前商品总价格"> </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="订单支付">
          <el-tag effect="dark" type="danger" size="mini" v-if="orderStateById.pay_status === '0'">未支付</el-tag>
          <div v-else>
            <el-tag type="success" size="mini">{{ mode }}</el-tag>
          </div>
        </el-form-item>
        <el-form-item label="是否发货">
          <span>{{ orderStateById.is_send }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
// 省市区数据
import cityData from '@/assets/js/citydata.js'
// 模拟的物流信息
import progresss from '@/assets/js/progress.js'

export default {
  name: 'Orders',
  data() {
    return {
      // 存储订单列表数据
      orderlist: [],
      // 订单列表的总数
      total: 0,
      // 请求订单列表时携带的参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 控制修改地址对话框显示与隐藏
      editAddressDialogVisible: false,
      // 修改地址表单对象
      editAddressForm: {
        address1: [],
        address2: ''
      },
      // 修改地址表单验证规则对象
      editAddressFormRules: {
        address1: [{ required: true, message: '请选择省市区/县', trigger: 'blur' }],
        address2: [{ required: true, message: '请填写详细地址', trigger: 'blur' }]
      },
      // 省市区数据
      cityData: cityData,
      // 修改地址的级联选择器配置对象
      editProps: {
        expandTrigger: 'hover'
      },
      // 控制物流信息对话的显示与隐藏
      ProgressDialogVisible: false,
      // 物流信息
      progresslist: [],
      // 控制订单状态对话框显示与隐藏
      detailVisible: false,
      // 根据ID查询出来的订单状态
      orderStateById: {},
      orderStateForm: {
        is_send: 0, // 订单是否发货 1:已经发货，0:未发货
        order_pay: 0, // 订单支付方式  0 未支付 1 支付宝 2 微信 3 银行卡
        order_price: 0, // 订单价格
        order_number: 1, // 订单数量
        pay_status: 0 // 支付状态  0 未付款、1 已付款
      }
    }
  },
  methods: {
    // 初始化订单列表数据
    async initOrdersList() {
      const { data: res } = await this.$http({
        method: 'GET',
        url: '/orders',
        params: this.queryInfo
      })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取订单列表数据失败')
      this.orderlist = res.data.goods
      this.total = res.data.total
    },
    // 点击了搜索按钮
    searchOrder() {
      this.initOrdersList()
    },
    // 清空输入框的内容
    clearIpt() {
      this.initOrdersList()
    },
    // 每页显示几条数据发生变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.initOrdersList()
    },
    // 当前页码发生变化
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.initOrdersList()
    },
    // 监听修改地址对话框关闭事件
    editAddressDialogClose() {
      this.$refs.editAddressFormRef.resetFields()
    },
    // 展示物流信息的对话框
    showProgressDialog(row) {
      // 接口已失效
      // const { data: res } = await this.$http({
      //   method: 'GET',
      //   url: '/kuaidi/' + 1106975712662
      // })
      // console.log(res)
      // if (res.meta.status !== 200) return this.$message.error('获取物流信息失败')
      this.progresslist = progresss
      // console.log(this.progresslist)
      this.ProgressDialogVisible = true
    },
    // 展示订单状态对话框
    async showDetailDialog(row) {
      const { data: res } = await this.$http({
        method: 'GET',
        url: '/orders/' + row.order_id
      })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取订单状态失败')
      this.orderStateById = res.data
      // console.log(this.orderStateById)
      this.detailVisible = true
    }
  },
  computed: {
    mode() {
      return this.orderStateById.order_pay === '1' ? '支付宝' : this.orderStateById.order_pay === '2' ? '微信' : '银行卡'
    }
  },
  created() {
    this.initOrdersList()
  }
}
</script>

<style lang="less" scoped>
.search {
  width: 30%;
}
.el-cascader {
  width: 100%;
}
</style>
